package com.dhcc.android.base.common.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.common.view.wheel.OnWheelChangedListener;
import com.dhcc.android.base.common.view.wheel.StrericWheelAdapter;
import com.dhcc.android.base.common.view.wheel.WheelView;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.StringUtil;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 日期时间选择
 *
 * @author zhoudan
 * @date 2015年8月5日
 */
public class DateTimePickerView {

    private int START_YEAR = 1990, END_YEAR = 2200;
    private String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
    private String[] months_little = {"4", "6", "9", "11"};
    final List<String> list_big = Arrays.asList(months_big);
    final List<String> list_little = Arrays.asList(months_little);
    private Activity activity;
    private View view;
    private WheelView wYear, wMonth, wDay, wHour, wMinute, wSecond;
    private int year, month, day, hour, minute, second;
    private AlertDialog dialog;
    private int type;//1：日期  2：时刻 3：年份

    public DateTimePickerView(Activity activity, int type) {
        this.activity = activity;
        this.type = type;
    }


    public void show(final TextView target) {
        getInitTime(target);
        initView();
        dialog = new AlertDialog.Builder(activity)
                .setTitle("时间选择")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        target.setText(getTimeFMTString());
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }

    private void initView() {

        view = LayoutInflater.from(activity).inflate(R.layout.util_date_time_picker_view, null);
        wYear = (WheelView) view.findViewById(R.id.wheel_year);
        wMonth = (WheelView) view.findViewById(R.id.wheel_month);
        wDay = (WheelView) view.findViewById(R.id.wheel_day);
        wHour = (WheelView) view.findViewById(R.id.wheel_hour);
        wMinute = (WheelView) view.findViewById(R.id.wheel_minute);
        wSecond = (WheelView) view.findViewById(R.id.wheel_second);

        switch (type) {
            case 1://日期 年月日
                setVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE, View.GONE, View.GONE, View.GONE);
                break;
            case 2://时刻年月日时分秒
                setVisible(View.VISIBLE, View.VISIBLE, View.VISIBLE, View.VISIBLE, View.VISIBLE, View.VISIBLE);
                break;
            case 3://年
                setVisible(View.VISIBLE, View.GONE, View.GONE, View.GONE, View.GONE, View.GONE);
                break;
            default:
                break;
        }
        wYear.setAdapter(new StrericWheelAdapter(getContent(110, START_YEAR)));
        wYear.setCurrentItem(year - START_YEAR);

        wMonth.setAdapter(new StrericWheelAdapter(getContent(12, 1)));
        wMonth.setCurrentItem(month - 1);

        dayContentChange(month, year);

        wHour.setAdapter(new StrericWheelAdapter(getContent(24, 0)));
        wHour.setCurrentItem(hour);

        wMinute.setAdapter(new StrericWheelAdapter(getContent(60, 0)));
        wMinute.setCurrentItem(minute);

        wSecond.setAdapter(new StrericWheelAdapter(getContent(60, 0)));
        wSecond.setCurrentItem(second);

        wYear.addChangingListener(yearchange);
        wMonth.addChangingListener(monthchange);
        setCyclic(true);
    }

    private void setVisible(int isYear, int isMonth, int isDay, int isHour, int isMinute, int isSecond) {
        wYear.setVisibility(isYear);
        wMonth.setVisibility(isMonth);
        wDay.setVisibility(isDay);
        wHour.setVisibility(isHour);
        wMinute.setVisibility(isMinute);
        wSecond.setVisibility(isSecond);
    }

    OnWheelChangedListener yearchange = new OnWheelChangedListener() {

        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            int ny = START_YEAR + newValue;
            int nm = wMonth.getCurrentItem() + 1;
            dayContentChange(nm, ny);
        }
    };

    OnWheelChangedListener monthchange = new OnWheelChangedListener() {

        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            int ny = wYear.getCurrentItem() + START_YEAR;
            int nm = newValue + 1;
            dayContentChange(nm, ny);
        }
    };


    private void dayContentChange(int month, int year) {
        int max = 30;
        if (list_big.contains(String.valueOf(month))) {
            max = 31;
            wDay.setAdapter(new StrericWheelAdapter(getContent(31, 1)));
        } else if (list_little.contains(String.valueOf(month))) {
            max = 30;
            wDay.setAdapter(new StrericWheelAdapter(getContent(30, 1)));
        } else {
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { //闰年
                max = 29;
                wDay.setAdapter(new StrericWheelAdapter(getContent(29, 1)));
            } else {
                max = 28;
                wDay.setAdapter(new StrericWheelAdapter(getContent(28, 1)));
            }
        }

        if (wDay.getCurrentItem() == 0) {
            wDay.setCurrentItem(day - 1);
        } else if (wDay.getCurrentItem() > max - 1) {
            wDay.setCurrentItem(max - 1);
        }
    }

    public void setCyclic(boolean isCyclic) {
        wYear.setCyclic(isCyclic);
        wMonth.setCyclic(isCyclic);
        wDay.setCyclic(isCyclic);
        wHour.setCyclic(isCyclic);
        wMinute.setCyclic(isCyclic);
        wSecond.setCyclic(isCyclic);
    }

    private String getTimeFMTString() {
        String time = "";
        switch (type) {
            case 1://日期 年月日
                time = wYear.getCurrentItem() + START_YEAR + "-"
                        + CommenUtil.formatterZero(wMonth.getCurrentItem() + 1) + "-"
                        + CommenUtil.formatterZero(wDay.getCurrentItem() + 1);
                break;
            case 2://时刻年月日时分秒
                time = wYear.getCurrentItem() + START_YEAR + "-"
                        + CommenUtil.formatterZero(wMonth.getCurrentItem() + 1) + "-"
                        + CommenUtil.formatterZero(wDay.getCurrentItem() + 1) + " "
                        + CommenUtil.formatterZero(wHour.getCurrentItem()) + ":"
                        + CommenUtil.formatterZero(wMinute.getCurrentItem()) + ":"
                        + CommenUtil.formatterZero(wSecond.getCurrentItem());
                break;
            case 3://年
                time = wYear.getCurrentItem() + START_YEAR + "";
                break;
            default:
                break;
        }
        return time;
    }

    private void getInitTime(TextView target) {
        String time = target.getText().toString();
        if (StringUtil.isEmpty(time)) {
            Calendar cal = Calendar.getInstance(Locale.CHINA);
            year = cal.get(Calendar.YEAR);
            month = cal.get(Calendar.MONTH);
            day = cal.get(Calendar.DAY_OF_MONTH);
            hour = cal.get(Calendar.HOUR_OF_DAY);
            minute = cal.get(Calendar.MINUTE);
            second = cal.get(Calendar.SECOND);
        } else {
            getDateTime(time);
        }
    }

    /**
     * 解析初始日期
     */
    private void getDateTime(String dt) {
        String[] dts = dt.split(" ");
        String[] ds = dts[0].split("-");
        if (dts.length > 1) {
            String[] ts = dts[1].split(":");
            hour = Integer.parseInt(ts[0]);
            minute = Integer.parseInt(ts[1]);
            second = Integer.parseInt(ts[2]);
        }
        if (ds.length > 1) {
            month = Integer.parseInt(ds[1]);
            day = Integer.parseInt(ds[2]);
        }
        year = Integer.parseInt(ds[0]);

    }

    private String[] getContent(int length, int start) {
        String[] s = new String[length];

        for (int i = 0; i < length; i++) {
            s[i] = String.valueOf(start + i);
            if (s[i].length() < 2) {
                s[i] = "0" + s[i];
            }
        }

        return s;
    }
}
